<template>
  <div>
    <div class="flex flex-col h-full">
      <SearchContainer @search="queryList" @reset="() => (formState = {})">
        <a-form :model="formState" class="form-style-flex">
          <a-form-item name="pyjm" class="w-1/3">
            <a-input v-model:value="formState.pyjm" placeholder="诊断代码/诊断名称/助记码" />
          </a-form-item>
        </a-form>
      </SearchContainer>
      <a-card class="mt-2 flex-1">
        <div class="flex justify-end">
          <BaseExport type="运维" class="mb-3" name="临床诊断" code="00001185" :search-params="formState">导出</BaseExport>
        </div>
        <BaseTable
          :loading="loading"
          column-code="00001185"
          fill
          :data-source="tableData"
          bordered
          :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
          @change="onPageChange"
        >
          <template #bodyCell="{ column, index, record }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'ztflag'">
              <a-switch v-model:checked="record.ztflag" checked-value="0" un-checked-value="1" @change="onHandleDeactivate(record.zddm, $event)" />
            </template>
            <template v-if="column.dataIndex === 'fbflag'">
              {{ record.fbflag === '1' ? '发布' : '未发布' }}
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const loading = ref(false)
const tableData = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })

const formState = ref({})
const onHandleDeactivate = async (zddm, number) => {
  if (number === '0') {
    const { code, msg } = await post('/operation/opex/doEnableCD10', { zddm })
    if (code !== 0) return message.error(msg)
    message.success(msg)
  } else {
    const { code, msg } = await post('/operation/opex/doDisableCD10', { zddm })
    if (code !== 0) return message.error(msg)
    message.success(msg)
  }
  queryList()
}

const queryList = async () => {
  loading.value = true
  // formState.value
  const { code, data, total } = await post('/operation/opex/getCD10InfoList', {
    pyjm: formState.value.pyjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  if (code !== 0) return
  tableData.value = data.map((item) => {
    item.ztflag = item.ztflag ? '1' : '0'
    return item
  })
  loading.value = false
  page.value.total = total
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
onMounted(() => {
  queryList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
